package com.easylinkin.linkappapi.asset.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.easylinkin.linkappapi.annotation.CommonOperateLogAnnotate;
import com.easylinkin.linkappapi.annotation.ObjectOperationLog;
import com.easylinkin.linkappapi.asset.entity.*;
import com.easylinkin.linkappapi.asset.service.AssetService;
import com.easylinkin.linkappapi.common.model.RequestModel;
import com.easylinkin.linkappapi.device.entity.Device;
import com.easylinkin.linkappapi.operatelog.constant.LogConstant.LogModule;
import com.easylinkin.linkappapi.operatelog.constant.LogConstant.LogOperateType;
import com.easylinkin.linkappapi.space.service.LinkappAreaService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 设备资产表，关联两张表 关联1  资产表-资产设备表 关联2  资产表-资产用户表 前端控制器
 * </p>
 *
 * @author XuGuoQing
 * @since 2020-04-15
 */
@RestController
@Api(value = "设备资产管理", tags = {"设备资产管理"})
@RequestMapping("/asset")
public class AssetController {


    private static final int ON_LINE = 1;
    private static final int OFF_LINE = 0;
    @Resource
    private AssetService assetService;

    @ApiOperation("新增资产")
    @CommonOperateLogAnnotate(module = LogModule.ASSET, desc = "新增资产")
    @PostMapping("/addLinkappAsset")
    @ObjectOperationLog(module = LogModule.ASSET, desc = LogOperateType.ASSET_ADD)
    public RestMessage addLinkappArea(@RequestBody Asset asset) {
        return RestBuilders.successBuilder().success(assetService.addLinkappAsset(asset)).build();
    }

    @CommonOperateLogAnnotate(module = LogModule.ASSET, desc = "修改资产")
    @ApiOperation("修改资产")
    @PostMapping("/updateLinkappAsset")
    @ObjectOperationLog(module = LogModule.ASSET, desc = LogOperateType.ASSET_UPDATE)
    public RestMessage updateLinkappAsset(@RequestBody Asset asset) {
        return RestBuilders.successBuilder().success(assetService.updateLinkappAsset(asset)).build();
    }


    @ApiOperation("删除资产")
    @CommonOperateLogAnnotate(module = LogModule.ASSET, desc = "删除资产")
    @PostMapping("/deleteLinkappAsset")
    @ObjectOperationLog(module = LogModule.ASSET, desc = LogOperateType.ASSET_DELETE)
    public RestMessage deleteLinkappAsset(@RequestBody List<Asset> assetList) {
        return RestBuilders.successBuilder().success(assetService.deleteLinkappAsset(assetList)).build();
    }

    @ApiOperation("某个资产详情")
    @PostMapping("/queryLinkappAssetById")
    public RestMessage queryLinkappAssetById(@RequestBody Asset asset) {
        asset = assetService.queryLinkappAssetById(asset);
        return RestBuilders.successBuilder().data(asset).build();
    }

    @ApiOperation("某个资产下面的设备分页")
    @PostMapping("/getDvicePageByAssetId")
    public RestMessage getDvicePageByAssetId(@RequestBody RequestModel<AdeviceVo> requestModel) {
        AdeviceVo adeviceVo = requestModel.getCustomQueryParams();
        Assert.notNull(adeviceVo, "查询参数不能为空");
        Assert.notNull(adeviceVo.getAssetId(), "资产id不能为空");
        Assert.notNull(adeviceVo.getFlag(), "查询标志位不能为空");
        //通过资产id 获得我们的区域id
        Asset asset = assetService.getById(adeviceVo.getAssetId());
        Assert.notNull(asset, "资产不能不存在");
        Assert.notNull(asset.getAssetAreaId(), "资产的位置不能为空");
        IPage<Device> record = assetService.getDvicePageByAssetId(requestModel.getPage(), adeviceVo);
        return RestBuilders.successBuilder().data(record).build();
    }

    @ApiOperation("某个资产没有关联设备分页")
    @PostMapping("/getUnCoonectDvicePageByAreaId")
    public RestMessage getUnCoonectDvicePageByAssetId(@RequestBody RequestModel<AdeviceVo> requestModel) {
        AdeviceVo adeviceVo = requestModel.getCustomQueryParams();
        Assert.notNull(adeviceVo, "查询参数不能为空");
        IPage<Device> record = assetService.getUnCoonectDvicePageByAssetId(requestModel.getPage(), adeviceVo);
        return RestBuilders.successBuilder().data(record).build();
    }

    @ApiOperation("资产列表")
    @PostMapping("/getPage")
    public RestMessage getPage(@RequestBody RequestModel<Asset> requestModel) {
        Assert.notNull(requestModel.getCustomQueryParams(), "customQueryParams 不能为空");
        Assert.notNull(requestModel.getPage(), "page 不能为空");
        IPage<Asset> record = assetService.getPage(requestModel.getPage(), requestModel.getCustomQueryParams());
        return RestBuilders.successBuilder().data(record).build();
    }


    //
    @ApiOperation("设备对应资产的详情")
    @PostMapping("/queryAssetAndDeviceInfo")
    public RestMessage queryAssetAndDeviceInfo(@RequestBody AdeviceVo adeviceVo) {
        DeviceInfo deviceInfo = new DeviceInfo();
        Asset asset = assetService.getById(adeviceVo.getAssetId());
        Assert.notNull(asset, "资产不能不存在");
        List<Device> deviceList = assetService.getDviceByAssetId(adeviceVo);
        if (deviceList != null && !deviceList.isEmpty()) {
            Map<String, Long> deviceTypeGroupMap = deviceList.stream().map(device -> {
                if (StringUtils.isBlank(device.getDeviceTypeName())) {
                    device.setDeviceTypeName("未知类型");
                }
                return device;
            }).collect(Collectors.groupingBy(Device::getDeviceTypeName, Collectors.counting()));
            List onlineList = deviceList.stream().filter((Device d) -> (d.getOnlineState() != null && d.getOnlineState().intValue() == ON_LINE)).collect(Collectors.toList());
            int totalnum = deviceList.size();
            int onLine = onlineList.size();
            deviceInfo = new DeviceInfo(totalnum, onLine, deviceTypeGroupMap);
        }
        asset = assetService.queryLinkappAssetById(asset);
        AssetAndDeviceInfo assetAndDeviceInfo = new AssetAndDeviceInfo(deviceInfo, asset);
        return RestBuilders.successBuilder().data(assetAndDeviceInfo).build();
    }


    @ApiOperation("管理员列表")
    @PostMapping("/queryManager")
    public RestMessage queryAssetAndDeviceInfo() {
        List<UserVo> userlist = assetService.queryManager();
        return RestBuilders.successBuilder().data(userlist).build();
    }


    @ApiOperation("查询资产日新增和月新增")
    @PostMapping("/queryDayAndMonthCreates")
    public RestMessage queryDayAndMonthCreates() {
        Map<String, Integer> counts = assetService.queryDayAndMonthCreates();
        return RestBuilders.successBuilder().data(counts).build();
    }


}
